package com.lishang.hooklocation

import android.app.AndroidAppHelper
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Environment
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.edit
import de.robv.android.xposed.IXposedHookInitPackageResources
import de.robv.android.xposed.IXposedHookLoadPackage
import de.robv.android.xposed.callbacks.XC_InitPackageResources
import de.robv.android.xposed.callbacks.XC_LayoutInflated
import de.robv.android.xposed.callbacks.XC_LoadPackage
import java.io.File
import java.math.BigDecimal

class HookModel : IXposedHookLoadPackage, IXposedHookInitPackageResources {
    override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) {
    }


    private fun log(msg: String) {
        Log.e("HookModel", msg)
    }

    private fun isHook(packName: String): Boolean = "com.cataclysm.i" == packName

    override fun handleInitPackageResources(resparam: XC_InitPackageResources.InitPackageResourcesParam) {
        if (isHook(resparam.packageName)) {
            log("hook com.cataclysm.i")

            resparam.res.hookLayout(resparam.packageName, "layout", "activity_guide", object :
                XC_LayoutInflated() {
                override fun handleLayoutInflated(liparam: LayoutInflatedParam) {

                    liparam.view.apply {
                        val root = this as ViewGroup

                        for (i in 0..root.childCount) {
                            if (root.getChildAt(i) is RelativeLayout) {
                                root.addView(LinearLayout(context).apply {
                                    orientation = LinearLayout.VERTICAL
                                    gravity = Gravity.CENTER

                                    val text = TextView(context).apply {
                                        setTextColor(Color.RED)
                                    }
                                    addView(text, -2, -2)

                                    addView(LinearLayout(context).apply {
                                        addView(Button(context).apply {
                                            this.text = "数据1"
                                            setOnClickListener {

                                                text.text = am(context)
                                            }
                                        })
                                        addView(View(context), 20, 20)
                                        addView(Button(context).apply {
                                            this.text = "数据2"
                                            setOnClickListener {

                                                text.text = pm(context)
                                            }
                                        })

                                    }, -2, -2)
                                }, -1, -2)
                            }
                            break
                        }

                    }
                }
            })
        }
    }

    private fun am(context: Context): String {
//        val min = XLocation(114.41942963591143,30.473331906477714)
//        val max = XLocation(114.42172399741594,30.473331906477714)
//
//        val value =
//            randomLonLat(
//                min.lng,
//                max.lng,
//                min.lat,
//                max.lat
//            )
//        putSP(context, value)
        return pm(context)
    }

    private fun pm(context: Context): String {
        val min = XLocation(114.420500, 30.473900)
        val max = XLocation(114.421586, 30.473693)

        val value =
            randomLonLat(
                context,
                min.lng,
                max.lng,
                min.lat,
                max.lat
            )
        putSP(context, value)

        return value
    }


    private fun putSP(context: Context, value: String) {

        context.getSharedPreferences("com.cataclysm.i_preferences", Context.MODE_PRIVATE).apply {
            edit(true) {
                putString("key299", value)
            }
        }


        File(
            Environment.getDataDirectory(),
            "data/com.cataclysm.i/shared_prefs/com.cataclysm.i_preferences.xml"
        ).setReadable(true, false)

        context.sendBroadcast(Intent("lataclysm").apply {
//            putExtra("lat", lat)
//            putExtra("lng", lon)
            putExtra("changed", "1")
        })

    }


    /**
     * @Title: randomLonLat
     * @Description: 在矩形内随机生成经纬度
     * @param MinLon：最新经度  MaxLon： 最大经度   MinLat：最新纬度   MaxLat：最大纬度    type：设置返回经度还是纬度
     * @return
     * @throws
     */
    fun randomLonLat(
        context: Context,
        MinLon: Double,
        MaxLon: Double,
        MinLat: Double,
        MaxLat: Double
    ): String {
        var db = BigDecimal(Math.random() * (MaxLon - MinLon) + MinLon)
        val lon: String = db.setScale(6, BigDecimal.ROUND_HALF_UP).toString() // 小数后6位
        db = BigDecimal(Math.random() * (MaxLat - MinLat) + MinLat)
        val lat: String = db.setScale(6, BigDecimal.ROUND_HALF_UP).toString()

        return "$lat,$lon,0,0,0,位置1"
    }

}

data class XLocation(val lng: Double, val lat: Double)